import { Dialog, Toast } from 'mand-mobile'
import { getMethod }   from '@/api/config'
import {config}  from '@/api/config'
import axios from 'axios'
import router from '@/router'
import store from "@/tools/factory"

var service = axios.create({
    baseURL: config.baseURL,
    timeout: 10000
})
function getPosition() {
    return new Promise((resolve, reject) => {
        let dataInfo = {
            lat: 29.8172,
            lng: 121.547
        }
        resolve(dataInfo)
        /*if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                let latitude = position.coords.latitude
                let longitude = position.coords.longitude
                let dataInfo = {
                    latitude: latitude,
                    longitude: longitude
                }
                resolve(dataInfo)
            }, function () {
                reject(arguments)
            })
        } else {
            reject('你的浏览器不支持当前地理位置信息获取')
        }*/
    })
}
function getPosition1() {
    return new Promise((resolve, reject) => {
        let dataInfo = {
            latitude: 29.8175,
            longitude: 121.547
        }
        resolve(dataInfo)
    })
}
function getUrlParams(url) {
    let paramsObj = {}
    let paramsArray
    paramsArray = url.split("?")[1].split("&")
    paramsArray.map(v => paramsObj[v.split("=")[0]] = v.split("=")[1])
    return paramsObj
}
let wx = {
    showToast: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let dataPercent = {}
        for (let key in dataInfo) {
            console.log(key, dataInfo[key], 'key')
            if (key == 'title') {
                dataPercent['content'] = dataInfo[key]
            } else {
                dataPercent[key] = dataInfo[key]
            }
        }
        Toast.failed(dataPercent)
    },
    showLoading: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let dataPercent = {}
        for (let key in dataInfo) {
            console.log(key, dataInfo[key], 'key')
            if (key == 'title') {
                dataPercent['content'] = dataInfo[key]
            } else {
                dataPercent[key] = dataInfo[key]
            }
        }
        Toast.loading(dataPercent.content)
    },
    hideLoading: function () {
        Toast.hide()
    },
    showModal: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let dataPercent = {}, switchText = ''
        for (let key in dataInfo) {
            dataPercent[key] = dataInfo[key]
            if (key == 'showCancel') {
                if (!dataInfo[key]) {
                    switchText = 'alert'
                } else {
                    switchText = 'confirm'
                }
                dataPercent['cancelText'] = '取消'
            } else if (key == 'success') {
                dataPercent.onConfirm = function (res) {
                    console.log(res, 'zheshires')
                    dataInfo.success(res)
                }
            } else if (key == 'fail') {
                dataPercent.onCancel = function (res) {
                    dataInfo.fail(res)
                }
            } else {
                dataPercent[key] = dataInfo[key]
            }
        }
        if (switchText = 'alert') {
            Dialog.alert(dataPercent)
        } else if (switchText = 'confirm') {
            Dialog.confirm(dataPercent)
        }
    },
    makePhoneCall: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        for (let key in dataInfo) {
            if (key == 'phoneNumber') {
                const a = document.createElement('a')
                a.setAttribute('href', '')// href链接
                a.setAttribute('target', '_self')// href链接
                a.href = 'tel:' + dataInfo.phoneNumber
                a.click()// 自执行点击事件
                console.log(dataInfo.phoneNumber, '拨号')
            }
        }

    },
    getLocation: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        getPosition1().then(res => {
            dataInfo.complete(res)
        }).catch((err) => {
            dataInfo.fail(err)
        })
    },
    request: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let { method, url, header, data = {} } = dataInfo
        // console.log(method.toUpperCase(), url, 'opp')
        service({
            method: method.toUpperCase(),
            url: url,
            [getMethod(method.toUpperCase())]: data,
            headers: header,
        }).then(res => {  //axios返回的是一个promise对象
            dataInfo.success(res)
        }).catch(err => {
            dataInfo.fail(err)
        })
    },
    navigateTo: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let { url } = dataInfo
        // console.log(url.split("?")[1].split("&"), '>>>>')
        let path = url.split('?')[0]
        try {
            router.push({
                path: path,
                query: getUrlParams(url)
            })
        } catch (e) {
            router.push({
                path: path,
            })
        }

    },
    navigateBack: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let { delta } = dataInfo
        router.back(delta)
    },
    redirectTo: function (dataInfo) {
        if (!Object.keys(dataInfo).length) {
            return
        }
        let { url } = dataInfo
        let path = url.split('?')[0]
        try {
            router.replace({
                path: path,
                query: getUrlParams(url)
            })
        } catch (e) {
            router.replace({
                path: path,
            })
        }
    },
    removeStorageSync: function (dataInfo) {
        store.remove(dataInfo)
    },
    setStorages: function (key, dataInfo) {
        store.set(key, dataInfo)
    }
}

export default wx
